package org.gdstash.ui.table;

import org.gdstash.file.DDSLoader;
import org.gdstash.item.GDItem;
import org.gdstash.ui.GDStashFrame;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class GDItemSetCollectionTableModel extends DefaultTableModel {
    private List<GDItemSetInfo> data;
    private GDItemSetCollectionRow[] rowsAll;
    private GDItemSetCollectionRow[] rows;

    public GDItemSetCollectionTableModel(final List<GDItemSetInfo> data) {
        this.data = data;
        this.adjustUI();
    }

    public void adjustUI() {
        GDItemCollectionRow.updateColumnNames();
        this.setColumnIdentifiers(GDItemCollectionRow.columnNames);
        this.setData(this.data);
    }

    public void setData(final List<GDItemSetInfo> data) {
        this.data = data;
        this.rowsAll = null;
        this.rows = null;
        if (data == null) {
            return;
        }
        final int size = data.size();
        this.rowsAll = new GDItemSetCollectionRow[size];
        this.rows = new GDItemSetCollectionRow[size];
        final GDItem.LabelInfo liEmpty = new GDItem.LabelInfo();
        liEmpty.text = "";
        int i = 0;
        final GDItem.LabelInfo li = null;
        for (final GDItemSetInfo set : data) {
            this.rowsAll[i] = new GDItemSetCollectionRow();
            this.rows[i] = this.rowsAll[i];
            this.rowsAll[i].set = set;
            this.rows[i].setName = set.getItemSetNameLabel();
            this.rows[i].setLevel = set.getItemSetLevelLabel();
            this.rows[i].setRarity = set.getItemSetRarityLabel();
            this.rows[i].level = set.getItemSetLevel();
            this.rows[i].rarity = set.getItemSetRarityInt();
            this.rowsAll[i].iconItem1 = null;
            this.rowsAll[i].scItem1 = -1;
            this.rowsAll[i].hcItem1 = -1;
            this.rowsAll[i].blueprint1 = false;
            this.rowsAll[i].iconItem2 = null;
            this.rowsAll[i].scItem2 = -1;
            this.rowsAll[i].hcItem2 = -1;
            this.rowsAll[i].blueprint2 = false;
            this.rowsAll[i].iconItem3 = null;
            this.rowsAll[i].scItem3 = -1;
            this.rowsAll[i].hcItem3 = -1;
            this.rowsAll[i].blueprint3 = false;
            this.rowsAll[i].iconItem4 = null;
            this.rowsAll[i].scItem4 = -1;
            this.rowsAll[i].hcItem4 = -1;
            this.rowsAll[i].blueprint4 = false;
            this.rowsAll[i].iconItem5 = null;
            this.rowsAll[i].scItem5 = -1;
            this.rowsAll[i].hcItem5 = -1;
            this.rowsAll[i].blueprint5 = false;
            this.rowsAll[i].iconItem6 = null;
            this.rowsAll[i].scItem6 = -1;
            this.rowsAll[i].hcItem6 = -1;
            this.rowsAll[i].blueprint6 = false;
            int count = 0;
            for (final GDItemInfo info : set.getItemInfoList()) {
                BufferedImage img = info.getFullImage();
                if (img != null) {
                    if (GDStashFrame.iniConfig.sectUI.graphicScale != 100) {
                        final int w = img.getWidth() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
                        final int h = img.getHeight() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
                        img = DDSLoader.getScaledImage(img, w, h);
                    }
                    if (count == 0) {
                        this.rowsAll[i].iconItem1 = new ImageIcon(img);
                        this.rowsAll[i].scItem1 = info.scCount;
                        this.rowsAll[i].hcItem1 = info.hcCount;
                        this.rowsAll[i].blueprint1 = (info.scBlueprint != null || info.hcBlueprint != null);
                    }
                    if (count == 1) {
                        this.rowsAll[i].iconItem2 = new ImageIcon(img);
                        this.rowsAll[i].scItem2 = info.scCount;
                        this.rowsAll[i].hcItem2 = info.hcCount;
                        this.rowsAll[i].blueprint2 = (info.scBlueprint != null || info.hcBlueprint != null);
                    }
                    if (count == 2) {
                        this.rowsAll[i].iconItem3 = new ImageIcon(img);
                        this.rowsAll[i].scItem3 = info.scCount;
                        this.rowsAll[i].hcItem3 = info.hcCount;
                        this.rowsAll[i].blueprint3 = (info.scBlueprint != null || info.hcBlueprint != null);
                    }
                    if (count == 3) {
                        this.rowsAll[i].iconItem4 = new ImageIcon(img);
                        this.rowsAll[i].scItem4 = info.scCount;
                        this.rowsAll[i].hcItem4 = info.hcCount;
                        this.rowsAll[i].blueprint4 = (info.scBlueprint != null || info.hcBlueprint != null);
                    }
                    if (count == 4) {
                        this.rowsAll[i].iconItem5 = new ImageIcon(img);
                        this.rowsAll[i].scItem5 = info.scCount;
                        this.rowsAll[i].hcItem5 = info.hcCount;
                        this.rowsAll[i].blueprint5 = (info.scBlueprint != null || info.hcBlueprint != null);
                    }
                    if (count == 5) {
                        this.rowsAll[i].iconItem6 = new ImageIcon(img);
                        this.rowsAll[i].scItem6 = info.scCount;
                        this.rowsAll[i].hcItem6 = info.hcCount;
                        this.rowsAll[i].blueprint6 = (info.scBlueprint != null || info.hcBlueprint != null);
                    }
                }
                ++count;
            }
            ++i;
        }
        this.fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        if (this.rows == null) {
            return 0;
        }
        return this.rows.length;
    }

    @Override
    public int getColumnCount() {
        return GDItemSetCollectionRow.columnNames.length;
    }

    @Override
    public String getColumnName(final int column) {
        return GDItemSetCollectionRow.columnNames[column];
    }

    @Override
    public Object getValueAt(final int row, final int column) {
        if (this.rows == null) {
            return null;
        }
        if (row < 0) {
            return null;
        }
        if (row >= this.rows.length) {
            return null;
        }
        if (this.rows[row] == null) {
            return null;
        }
        if (column == 0) {
            return this.rows[row].setName.text;
        }
        if (column == 1) {
            return this.rows[row].setLevel.text;
        }
        if (column == 2) {
            return this.rows[row].setRarity.text;
        }
        if (column == 3) {
            return this.rows[row].iconItem1;
        }
        if (column == 4) {
            return this.rows[row].iconItem2;
        }
        if (column == 5) {
            return this.rows[row].iconItem3;
        }
        if (column == 6) {
            return this.rows[row].iconItem4;
        }
        if (column == 7) {
            return this.rows[row].iconItem5;
        }
        if (column == 8) {
            return this.rows[row].iconItem6;
        }
        return null;
    }

    @Override
    public boolean isCellEditable(final int row, final int column) {
        return false;
    }

    @Override
    public void setValueAt(final Object value, final int row, final int column) {
        if (!this.isCellEditable(row, column)) {
            return;
        }
        if (this.rows == null) {
            return;
        }
        if (row < 0) {
            return;
        }
        if (row >= this.rows.length) {
            return;
        }
        if (this.rows[row] == null) {
            return;
        }
        if (column == 0) {
            this.rows[row].setName.text = (String) value;
        }
        if (column == 1) {
            this.rows[row].setLevel.text = (String) value;
        }
        if (column == 2) {
            this.rows[row].setRarity.text = (String) value;
        }
        if (column == 3) {
            this.rows[row].iconItem1 = (ImageIcon) value;
        }
        if (column == 4) {
            this.rows[row].iconItem2 = (ImageIcon) value;
        }
        if (column == 5) {
            this.rows[row].iconItem3 = (ImageIcon) value;
        }
        if (column == 6) {
            this.rows[row].iconItem4 = (ImageIcon) value;
        }
        if (column == 7) {
            this.rows[row].iconItem5 = (ImageIcon) value;
        }
        if (column == 8) {
            this.rows[row].iconItem6 = (ImageIcon) value;
        }
    }

    @Override
    public Class getColumnClass(final int column) {
        return GDItemSetCollectionRow.COLUMN_CLASSES[column];
    }

    public String getCellText(final int row, final int column) {
        if (this.rows == null) {
            return "";
        }
        if (row < 0) {
            return "";
        }
        if (row >= this.rows.length) {
            return "";
        }
        if (this.rows[row] == null) {
            return "";
        }
        if (column == 0) {
            return this.rows[row].setName.text;
        }
        if (column == 1) {
            return this.rows[row].setLevel.text;
        }
        if (column == 2) {
            return this.rows[row].setRarity.text;
        }
        return "";
    }

    public Color getCellTextColor(final int row, final int column) {
        if (this.rows == null) {
            return Color.BLACK;
        }
        if (row < 0) {
            return Color.BLACK;
        }
        if (row >= this.rows.length) {
            return Color.BLACK;
        }
        if (this.rows[row] == null) {
            return Color.BLACK;
        }
        if (column == 0) {
            return this.rows[row].setName.foreground;
        }
        if (column == 1) {
            return this.rows[row].setLevel.foreground;
        }
        if (column == 2) {
            return this.rows[row].setRarity.foreground;
        }
        return Color.BLACK;
    }

    public Color getCellBackgroundColor(final int row, final int column) {
        if (this.rows == null) {
            return Color.WHITE;
        }
        if (row < 0) {
            return Color.WHITE;
        }
        if (row >= this.rows.length) {
            return Color.WHITE;
        }
        if (this.rows[row] == null) {
            return Color.WHITE;
        }
        if (column == 0) {
            return this.rows[row].setName.background;
        }
        if (column == 1) {
            return this.rows[row].setLevel.background;
        }
        if (column == 2) {
            return this.rows[row].setRarity.background;
        }
        if (column == 3) {
            if (this.rows[row].scItem1 > 0 || this.rows[row].hcItem1 > 0) {
                return GDItemInfo.COLOR_GREEN;
            }
            if (this.rows[row].scItem1 == -1) {
                return UIManager.getColor("Table.background");
            }
            if (this.rows[row].blueprint1) {
                return GDItemInfo.COLOR_YELLOW;
            }
            return GDItemInfo.COLOR_RED;
        } else if (column == 4) {
            if (this.rows[row].scItem2 > 0 || this.rows[row].hcItem2 > 0) {
                return GDItemInfo.COLOR_GREEN;
            }
            if (this.rows[row].scItem2 == -1) {
                return UIManager.getColor("Table.background");
            }
            if (this.rows[row].blueprint2) {
                return GDItemInfo.COLOR_YELLOW;
            }
            return GDItemInfo.COLOR_RED;
        } else if (column == 5) {
            if (this.rows[row].scItem3 > 0 || this.rows[row].hcItem3 > 0) {
                return GDItemInfo.COLOR_GREEN;
            }
            if (this.rows[row].scItem3 == -1) {
                return UIManager.getColor("Table.background");
            }
            if (this.rows[row].blueprint3) {
                return GDItemInfo.COLOR_YELLOW;
            }
            return GDItemInfo.COLOR_RED;
        } else if (column == 6) {
            if (this.rows[row].scItem4 > 0 || this.rows[row].hcItem4 > 0) {
                return GDItemInfo.COLOR_GREEN;
            }
            if (this.rows[row].scItem4 == -1) {
                return UIManager.getColor("Table.background");
            }
            if (this.rows[row].blueprint4) {
                return GDItemInfo.COLOR_YELLOW;
            }
            return GDItemInfo.COLOR_RED;
        } else if (column == 7) {
            if (this.rows[row].scItem5 > 0 || this.rows[row].hcItem5 > 0) {
                return GDItemInfo.COLOR_GREEN;
            }
            if (this.rows[row].scItem5 == -1) {
                return UIManager.getColor("Table.background");
            }
            if (this.rows[row].blueprint5) {
                return GDItemInfo.COLOR_YELLOW;
            }
            return GDItemInfo.COLOR_RED;
        } else {
            if (column != 8) {
                return Color.WHITE;
            }
            if (this.rows[row].scItem6 > 0 || this.rows[row].hcItem6 > 0) {
                return GDItemInfo.COLOR_GREEN;
            }
            if (this.rows[row].scItem6 == -1) {
                return UIManager.getColor("Table.background");
            }
            if (this.rows[row].blueprint6) {
                return GDItemInfo.COLOR_YELLOW;
            }
            return GDItemInfo.COLOR_RED;
        }
    }

    public void sort(final Comparator<GDItemSetCollectionRow> comp) {
        if (comp == null) {
            return;
        }
        if (this.rows == null) {
            return;
        }
        Arrays.<GDItemSetCollectionRow>sort(this.rows, comp);
    }
}
